import {
    TOKEN_KEY
} from '../constants/token-const'

const token = wx.getStorageSync(TOKEN_KEY)

const BASE_URL = "http://123.207.32.32:9001"
// 用我已经部署好的
const LOGIN_BASE_URL = "http://123.207.32.32:3000"

class XJHRequest {
    constructor(baseURL, authHeader = {}) {
        this.baseURL = baseURL
        this.authHeader = authHeader
    }
    request(url, method, params, isAuth = false, header = {}) {
        const finalHeader = isAuth ? { ...this.authHeader, ...header }: header
        return new Promise((resolve, reject) => {
            wx.request({
                url: this.baseURL + url,
                method: method,
                data: params,
                header: finalHeader,
                success(res) {
                    resolve(res.data)
                },
                fail: reject
            })
        })
    }
    get(url, params, isAuth = false, header) {
        return this.request(url, 'GET', params, isAuth, header)
    }
    post(url, data, isAuth = false, header) {
        return this.request(url, 'POST', data, isAuth, header)
    }
}

const xjhRequest = new XJHRequest(BASE_URL)
// 这样就可以很轻易的带上一个 token 这样,然后用的还是不一样的地址这样
const xjhLoginRequest = new XJHRequest(LOGIN_BASE_URL, {
    token
})

export default xjhRequest

export {
    xjhLoginRequest
}